<?php
namespace Home\Controller;

class XzController extends HomeController{
    
    public function index(){
        $page=I('request.cid',1);
        $pagesize=I('request.pagesize',8);
       
        
        $page<=0 && $page=1;
        $pagesize<=0 && $page=8;
        $offset=($page-1)*$pagesize;
        
        $M_product=M('product');
       
        $M_product_image=M('product_image');
        $map=array();
        $map['status']=1;
        $map['is_show']=1;
        $map['type']="xianzhi";
        
        $M_product_category=M('product_category');
        $cid=I('request.cid');
        $fid=I('request.fid');
        if($cid){
            $map['category_id']=$cid;
        }
        else if($fid){
            $cids=$M_product_category->where(array('pid'=>$fid))->field('id')->select();
            $cids=arr2tostr($cids);
            $map['category_id']=array('in',$cids);
        }
        
        
        $list=$M_product->limit($offset,$pagesize)->where($map)->order('id desc')->select();
        foreach ($list as &$item) {            
            $image_info = $M_product_image->where(array('product_id'=>$item['id']))->order('id ASC')->find();
             
            $item['image'] = get_cover($image_info['cover_id'],'path');             
        }
        $this->assign('p_list',$list);
      
        $map2=array();
        $map2['status']=1;
        $map2['is_show']=1;
        $map2['type']="xianzhi";
        $p_list_hot=$M_product->limit($offset,$pagesize)->where($map2)->order('view desc')->select();
        foreach ($p_list_hot as &$item) {
            $image_info = $M_product_image->where(array('product_id'=>$item['id']))->order('id ASC')->find();
             
            $item['image'] = get_cover($image_info['cover_id'],'path');
        }
        $this->assign('p_list_hot',$p_list_hot);
        //广告
        $ad_positon=M('ad_position')->where(array('identifier'=>'ad_homepagetop'))->find();
        
        $adlist=M('ad')->where(array('position_id'=>$ad_positon['id']))
        ->order('sort DESC')->field('cover_id,title,link,product_id')->select();
        foreach($adlist as &$item){
            $item['image']=get_cover($item['cover_id'],'path');
        }
        
        $this->assign('adlist',$adlist);        
        $this->display();
    }
    public function saveData(){
        if(IS_POST){
            $data=array();
        
            $data['title']=I('post.title');           
            $data['category_id']=I('post.category_id');
            $data['price']=I('post.price');
            $data['quantity']=I('post.quantity');
            $data['description']=I('post.description');
            $data['province_id']=I('post.province_id');
            $data['area_id']=I('post.area_id');
            $data['city_id']=I('post.city_id');
            $data['address']=I('post.address');
            $data['security_7days']=I('post.security_7days');
            $data['security_delivery']=I('post.security_delivery',0);
            $data['status']=I('post.status',1);
        
        
            $pzg_image_file=I('post.pzg_image_file');
            $pzg_movie_file=I('post.pzg_movie_file');
        
            $M_product = M('product');
            $M_shop = M('shop');
            $M_product_image = M('product_image');
        
            $data['uid'] = UID;
        
            !preg_match('/[\x{4e00}-\x{9fa5}A-Za-z0-9-_.\/\s]{4,60}+$/u',
                    $data['title']) && $this->error( '商品名称长度必须在5-60个字符之间');
            !get_table_field($data['category_id'], 'id', 'id', 'product_category') && $this->error('商品分类为空或没有找到匹配的分类！');
            !(is_numeric($data['price']) && $data['price'] > 0) && $this->error('请输入商品价格，不能小于1');
            !(preg_match('/[\d]+$/', $data['quantity']) && intval($data['quantity']) >= 0) && $this->error( '请输入商品库存，不能小于0');
            !get_table_field($data['province_id'], 'province_id', 'province_id', 'hat_province') && $this->error('请选择发货地的省份');
            !get_table_field($data['city_id'], 'city_id', 'city_id', 'hat_city') && $this->error('请选择发货地的城市');
            !get_table_field($data['area_id'], 'area_id', 'area_id', 'hat_area') && $this->error('请选择发货地的区县');
            !preg_match('/^[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5}A-Za-z0-9_-]{1,50}+$/u', $data['address']) && $this->error('详细地址长度必须在2~50个字符');
            !getStatus('security_7days', $data['security_7days']) && $this->error('支持7天无理由退货不能为空');
            !getStatus('security_delivery', $data['security_delivery']) && $this->error('承诺发货时间不能为空');
            !getStatus('product', $data['status']) && $this->error( '请选择商品状态');
             
            $data['type'] = 'xianzhi';
            $data['create_time'] = NOW_TIME;
            $data['update_time'] = NOW_TIME;
        
            $id = $M_product->add($data);
        
            if($id){
                $pics = I('post.pzg_image_file');
                foreach ($pics as $pic) {
                    $M_product_image->add(array('product_id'=>$id, 'cover_id'=>$pic));
                }
        
                $movie = I('post.pzg_movie_file');
                $M_product->where(array('id'=>$id))->save(array('movie'=>$movie));
        
                $this->error('发布成功');
            }
            else{
                $this->error('发布失败');
            }
        
        }
    }
    public function add(){     
        
        $this->assign('category_tree', $this->getCategoryTree());
        $this->display();
    }
    
    /**
     * 获取商品分类树
     *
     * @return Ambigous <multitype:, mixed>
     */
    private function getCategoryTree(){
        static $list = array();
    
        if (empty($list)) {
            $list = S('CACHE_ADMIN_PRODUCT_CATEGORY');
        }
    
        if (empty($list)) {
            $M_product_category = M('product_category');
            $list = $M_product_category->order('sort ASC')->select();
            $list = list_to_tree($list);
    
            S('CACHE_ADMIN_PRODUCT_CATEGORY', $list);
        }
    
        return $list;
    }
}
?>